home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / Examples / GUSITest.c < prev    next >
Text File  |  1995-04-06  |  5KB  |  270 lines

  1. /*********************************************************************
  2. File        :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSITest.c        -    Common testing gear
  4. Author    :    Matthias Neeracher <neeri@iis.ethz.ch>
  5. Language    :    MPW C
  6.  
  7. $Log: GUSITest.c,v $
  8. Revision 1.2  1994/12/31  01:15:09  neeri
  9. ANSIfy.
  10. Benchmark support.
  11.  
  12. Revision 1.1  1994/02/25  02:47:57  neeri
  13. Initial revision
  14.  
  15. Revision 0.2  1992/09/20  00:00:00  neeri
  16. Allow empty lines & comments
  17.  
  18. Revision 0.1  1992/09/08  00:00:00  neeri
  19. Factor out more common code
  20.  
  21. *********************************************************************/
  22.  
  23. #include <Memory.h>
  24. #include <QuickDraw.h>
  25. #include "GUSITest.h"
  26. #include <Types.h>
  27. #include <stdio.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <sys/errno.h>
  32.  
  33. #ifdef __MWERKS__
  34. #include <SIOUX.h>
  35. #endif
  36.  
  37. Boolean     HellHoundOnMyTrail = true;            /* Gotta keep on moving */
  38. char        infilename[200];
  39. char *    inputfrom;
  40. FILE *    input;
  41. int        inputline;
  42. CmdDef     commands[NROFCMDS];
  43.     
  44. void Help(char ch1, char ch2, const char * cmd)
  45. {    
  46.     printf("Commands are:\n\n");
  47.     
  48.     for (ch1 = 'a'; ch1 <= 'z'; ++ch1)
  49.         for (ch2 = 0; ch2 <= 'z'; ch2 ? ++ch2 : (ch2 = 'a'))
  50.             if (HELPMSG(ch1,ch2))
  51.                 printf(
  52.                     "\t%c%c %-25s -- %s\n", 
  53.                     ch1, 
  54.                     ch2 ? ch2 : ' ', 
  55.                     USAGE(ch1,ch2), 
  56.                     HELPMSG(ch1,ch2));
  57.     
  58.     printf("\n");
  59. }
  60.  
  61. void Where()
  62. {
  63.     if (inputfrom)
  64.         printf("File '%s'; Line %d\n", inputfrom, inputline);
  65. }
  66.  
  67. void Prompt()
  68. {
  69.     if (!inputfrom)
  70.         printf("[%d]%c", inputline, ' ');
  71. }
  72.  
  73. #define CASE(code)    case code: return #code
  74.  
  75. const char * Explain()
  76. {
  77.     switch (errno) {
  78.     CASE(EPERM);
  79.     CASE(ENOENT);
  80.     CASE(ESRCH);
  81.     CASE(EINTR);
  82.     CASE(EIO);
  83.     CASE(ENXIO);
  84.     CASE(E2BIG);
  85.     CASE(ENOEXEC);
  86.     CASE(EBADF);
  87.     CASE(ECHILD);
  88.     CASE(ENOMEM);
  89.     CASE(EACCES);
  90.     CASE(EFAULT);
  91.     CASE(ENOTBLK);
  92.     CASE(EBUSY);
  93.     CASE(EEXIST);
  94.     CASE(EXDEV);
  95.     CASE(ENODEV);
  96.     CASE(ENOTDIR);
  97.     CASE(EISDIR);
  98.     CASE(EINVAL);
  99.     CASE(ENFILE);
  100.     CASE(EMFILE);
  101.     CASE(ENOTTY);
  102.     CASE(ETXTBSY);
  103.     CASE(EFBIG);
  104.     CASE(ENOSPC);
  105.     CASE(ESPIPE);
  106.     CASE(EROFS);
  107.     CASE(EMLINK);
  108.     CASE(EPIPE);
  109.     CASE(EDOM);
  110.     CASE(ERANGE);
  111.     CASE(EWOULDBLOCK);
  112.     CASE(EINPROGRESS);
  113.     CASE(EALREADY);
  114.     CASE(ENOTSOCK);
  115.     CASE(EDESTADDRREQ);
  116.     CASE(EMSGSIZE);
  117.     CASE(EPROTOTYPE);
  118.     CASE(ENOPROTOOPT);
  119.     CASE(EPROTONOSUPPORT);
  120.     CASE(ESOCKTNOSUPPORT);
  121.     CASE(EOPNOTSUPP);
  122.     CASE(EPFNOSUPPORT);
  123.     CASE(EAFNOSUPPORT);
  124.     CASE(EADDRINUSE);
  125.     CASE(EADDRNOTAVAIL);
  126.     CASE(ENETDOWN);
  127.     CASE(ENETUNREACH);
  128.     CASE(ENETRESET);
  129.     CASE(ECONNABORTED);
  130.     CASE(ECONNRESET);
  131.     CASE(ENOBUFS);
  132.     CASE(EISCONN);
  133.     CASE(ENOTCONN);
  134.     CASE(ESHUTDOWN);
  135.     CASE(ETOOMANYREFS);
  136.     CASE(ETIMEDOUT);
  137.     CASE(ECONNREFUSED);
  138.     CASE(ELOOP);
  139.     CASE(ENAMETOOLONG);
  140.     CASE(EHOSTDOWN);
  141.     CASE(EHOSTUNREACH);
  142.     CASE(ENOTEMPTY);
  143.     CASE(EPROCLIM);
  144.     CASE(EUSERS);
  145.     CASE(EDQUOT);
  146.     CASE(ESTALE);
  147.     CASE(EREMOTE);
  148.     CASE(EBADRPC);
  149.     CASE(ERPCMISMATCH);
  150.     CASE(EPROGUNAVAIL);
  151.     CASE(EPROGMISMATCH);
  152.     CASE(EPROCUNAVAIL);
  153.     CASE(ENOLCK);
  154.     CASE(ENOSYS);
  155.     default:
  156.         return "Unknown";
  157.     }        
  158. }
  159.  
  160. void Usage(char ch1, char ch2)
  161. {
  162.     printf("# Usage is: %c%c %s\n", ch1, ch2 ? ch2 : ' ', USAGE(ch1,ch2));
  163.     Where();
  164. }
  165.  
  166. void Dispatch(const char * command)
  167. {
  168.     char        ch1    =    command[0];
  169.     char        ch2    =    command[1];
  170.     TestCmd    exec;
  171.     
  172.     /* We are guaranteed to have at least one valid character */
  173.     
  174.     switch (ch1) {
  175.     case '\n':
  176.     case '#':
  177.         return;
  178.     }
  179.  
  180.     if (!ch2)
  181.         ++command;
  182.     else {
  183.         if (isspace(ch2))    {
  184.             command    +=    1;
  185.             ch2        =    0;
  186.         } else
  187.             command    +=    2;
  188.         
  189.         /* Skip rest of first word */    
  190.         for (; *command && !isspace(*command); ++command);
  191.         
  192.         /* Skip whitespace */
  193.         while (isspace(*command))
  194.             ++command;
  195.     }
  196.     
  197.     if (isalpha(ch1) && (!ch2 || isalpha(ch2)) && (exec = DISPATCH(ch1,ch2)))
  198.         exec(ch1, ch2, command);
  199.     else {
  200.         if (ch2)
  201.             printf("# Unknown command: '%c%c'\n", ch1, ch2);
  202.         else
  203.             printf("# Unknown command: '%c'\n", ch1);
  204.             
  205.         printf("# Type 'h' for a list of known commands.\n"); 
  206.         
  207.         Where();
  208.     }
  209. }
  210.  
  211. void Quit(char ch1, char ch2, const char * cmd)
  212. {
  213.     HellHoundOnMyTrail = false;
  214. }
  215.  
  216. void InitSampler(Sampler * samp)
  217. {
  218.     samp->count    = 0;
  219.     samp->min   = 0x7FFFFFFF;
  220.     samp->max   = -samp->min;
  221.     samp->sum   = 0;
  222. }
  223.  
  224. void Sample(Sampler * samp, long sample)
  225. {
  226.     ++samp->count;
  227.     if (sample < samp->min)
  228.         samp->min = sample;
  229.     if (sample > samp->max)
  230.         samp->max = sample;
  231.     samp->sum += sample;
  232. }
  233.  
  234. void RunTest(int argc, char ** argv)
  235. {
  236.  
  237.     char         cmd[80];
  238.  
  239.     COMMAND('h',   0, Help,  "",                         "Print this list");
  240.     COMMAND('q',   0, Quit,  "",                         "End the sad existence of this program");
  241.  
  242. #ifdef __MWERKS__
  243.     SIOUXSettings.asktosaveonclose = false;
  244. #else
  245.     InitGraf((Ptr) &qd.thePort);
  246. #endif
  247.  
  248.     if (--argc <= 0)
  249.         Help('h', 0, "");
  250.     
  251.     do {
  252.         if (argc > 0 && strcmp(inputfrom = *++argv, "-"))    {
  253.             printf("Executing %s…\n", inputfrom);
  254.             input          =    fopen(inputfrom, "r");
  255.         } else {
  256.             inputfrom    =    0;
  257.             input            =    stdin;
  258.         }
  259.  
  260.         inputline    =    1;
  261.         
  262.         while (HellHoundOnMyTrail && (Prompt(), fgets(cmd, 80, input)))    {
  263.             Dispatch(cmd);
  264.             ++inputline;
  265.         }
  266.     } while (HellHoundOnMyTrail && --argc > 0);
  267.         
  268.     printf("So long, it's been good to know you.\n");
  269. }
  270.